/*
 * jNice
 * version: 1.0 (11.26.08)
 * by Sean Mooney (sean@whitespace-creative.com) 
 * Examples at: http://www.whitespace-creative.com/jquery/jnice/
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * To Use: place in the head 
 *  <link href="inc/style/jNice.css" rel="stylesheet" type="text/css" />
 *  <script type="text/javascript" src="inc/js/jquery.jNice.js"></script>
 *
 * And apply the jNice class to the form you want to style
 *
 * To Do: Add textareas, Add File upload
 *
 ******************************************** */
(function(jQuery){
	jQuery.fn.jNice = function(options){
		var self = this;
		var safari = jQuery.browser.safari; /* We need to check for safari to fix the input:text problem */
		/* Apply document listener */
		jQuery(document).mousedown(checkExternalClick);
		/* each form */
		return this.each(function(){
			jQuery('input:submit, input:reset, input:button', this).each(ButtonAdd);
			jQuery('button').focus(function(){ jQuery(this).addClass('jNiceFocus')}).blur(function(){ jQuery(this).removeClass('jNiceFocus')});
			jQuery('input:text:visible, input:password', this).each(TextAdd);
			/* If this is safari we need to add an extra class */
			if (safari){jQuery('.jNiceInputWrapper').each(function(){jQuery(this).addClass('jNiceSafari').find('input').css('width', jQuery(this).width()+11);});}
			jQuery('input:checkbox', this).each(CheckAdd);
			jQuery('input:radio', this).each(RadioAdd);
			/*jQuery('select', this).each(function(index){ SelectAdd(this, index); });*/
			/* Add a new handler for the reset action */
			jQuery(this).bind('reset',function(){var action = function(){ Reset(this); }; window.setTimeout(action, 10); });
			jQuery('.jNiceHidden').css({opacity:0});
		});		
	};/* End the Plugin */

	var Reset = function(form){
		var sel;
		jQuery('.jNiceSelectWrapper select', form).each(function(){sel = (this.selectedIndex<0) ? 0 : this.selectedIndex; jQuery('ul', jQuery(this).parent()).each(function(){jQuery('a:eq('+ sel +')', this).click();});});
		jQuery('a.jNiceCheckbox, a.jNiceRadio', form).removeClass('jNiceChecked');
		jQuery('input:checkbox, input:radio', form).each(function(){if(this.checked){jQuery('a', jQuery(this).parent()).addClass('jNiceChecked');}});
	};

	var RadioAdd = function(){
		var jQueryinput = jQuery(this).addClass('jNiceHidden').wrap('<span class="jRadioWrapper jNiceWrapper"></span>');
		var jQuerywrapper = jQueryinput.parent();
		var jQuerya = jQuery('<span class="jNiceRadio"></span>');
		jQuerywrapper.prepend(jQuerya);
		/* Click Handler */
		jQuerya.click(function(){
				var jQueryinput = jQuery(this).addClass('jNiceChecked').siblings('input').attr('checked',true);
				/* uncheck all others of same name */
				jQuery('input:radio[name="'+ jQueryinput.attr('name') +'"]').not(jQueryinput).each(function(){
					jQuery(this).attr('checked',false).siblings('.jNiceRadio').removeClass('jNiceChecked');
				});
				return false;
		});
		jQueryinput.click(function(){
			if(this.checked){
				var jQueryinput = jQuery(this).siblings('.jNiceRadio').addClass('jNiceChecked').end();
				/* uncheck all others of same name */
				jQuery('input:radio[name="'+ jQueryinput.attr('name') +'"]').not(jQueryinput).each(function(){
					jQuery(this).attr('checked',false).siblings('.jNiceRadio').removeClass('jNiceChecked');
				});
			}
		}).focus(function(){ jQuerya.addClass('jNiceFocus'); }).blur(function(){ jQuerya.removeClass('jNiceFocus'); });

		/* set the default state */
		if (this.checked){ jQuerya.addClass('jNiceChecked'); }
	};

	var CheckAdd = function(){
                
		var jQueryinput = jQuery(this).addClass('jNiceHidden').wrap('<span class="jNiceWrapper"></span>');
		var jQuerywrapper = jQueryinput.parent().append('<span class="jNiceCheckbox"></span>');
		/* Click Handler */
		var jQuerya = jQuerywrapper.find('.jNiceCheckbox').click(function(){
                        
                        var jQuerya = jQuery(this);
                        var input = jQuerya.siblings('input')[0];
                        var id = jQuery(input).attr("id");
                        if (input.checked===true){
                            
                             
                             
                                if(id == "same_as_billing"){
                                    jQuery("#shipping_id").show();
                                }
                                input.checked = false;
                                jQuerya.removeClass('jNiceChecked');
                        }
                        else {
                            
                                
                                
                                if(id == "same_as_billing"){
                                    jQuery("#shipping_id").hide();
                                }
                                
                                input.checked = true;
                                jQuerya.addClass('jNiceChecked');
                        }
                        return false;
		});
		jQueryinput.click(function(){
			if(this.checked){ jQuerya.addClass('jNiceChecked'); 	}
			else { jQuerya.removeClass('jNiceChecked'); }
		}).focus(function(){ jQuerya.addClass('jNiceFocus'); }).blur(function(){ jQuerya.removeClass('jNiceFocus'); });
		
		/* set the default state */
		if (this.checked){jQuery('.jNiceCheckbox', jQuerywrapper).addClass('jNiceChecked');}
	};

	var TextAdd = function(){
		var jQueryinput = jQuery(this).addClass('jNiceInput').wrap('<div class="jNiceInputWrapper"><div class="jNiceInputInner"></div></div>');
		var jQuerywrapper = jQueryinput.parents('.jNiceInputWrapper');
		jQueryinput.focus(function(){ 
			jQuerywrapper.addClass('jNiceInputWrapper_hover');
		}).blur(function(){
			jQuerywrapper.removeClass('jNiceInputWrapper_hover');
		});
	};

	var ButtonAdd = function(){
            
            var value = jQuery(this).attr('value');
            jQuery(this).replaceWith('<button id="'+ this.id +'" name="'+ this.name +'" type="'+ this.type +'" class="'+ this.className +'" value="'+ value +'"><span><span>'+ value +'</span></span>');
	};

	/* Hide all open selects */
	var SelectHide = function(){
            jQuery('.jNiceSelectWrapper ul:visible').hide();
	};
        
	/* Check for an external click */
        
	var checkExternalClick = function(event) {
            
            if (jQuery(event.target).parents('.jNiceSelectWrapper').length === 0) { SelectHide(); }
	};
    
	var SelectRemove = function(element){
		var zIndex = jQuery(element).siblings('.jNiceSelectWrapper').css('zIndex');
		jQuery(element).css({zIndex: zIndex}).removeClass('jNiceHidden');
		jQuery(element).siblings('.jNiceSelectWrapper').remove();
	};
    
	/* Utilities */
    /*
	jQuery.jNice = {
            
			SelectAdd : function(element, index){ 	SelectAdd(element, index); },
			SelectRemove : function(element){ SelectRemove(element); },
			SelectUpdate : function(element){ SelectUpdate(element); }
	};/* End Utilities */
   
	/* Automatically apply to any forms with class jNice */
	jQuery(function(){jQuery('form.jNice').jNice();	});
})(jQuery);

    